<template>
  <div class="search-box">
    <i class="icon-search"></i>
    <input type="text" class="box" :placeholder="placeholder" v-model="query" ref="query">
    <span class="cancel" @click="goBack()">取消</span>
  </div>
</template>

<script type="text/ecmascript-6">
export default {
  props: {
    placeholder: {
      type: String,
      default: "搜索旅行地、景点、酒店。"
    },
    tagSearch: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      query: ""
    };
  },
  methods: {
    goBack() {
      history.go(-1);
      this.$store.dispatch("showTabbar", { boolean: "0" });
    },
    setQuery(query) {
      this.query = query;
    },
    clear() {
      this.query = "";
    },
    blur() {
      this.$refs.query.blur();
    },
    focus() {
      this.$refs.query.focus();
    }
  },
  watch: {
    query(newQuery) {
      if (!this.tagSearch) {
        // 节流操作
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          this.$emit("query", newQuery);
        }, 200);
      } else {
        // 标签搜索不需要节流
        this.$emit("query", newQuery);
      }
    }
  }
};
</script>

<style scoped >
.search-box {
  box-sizing: border-box;
  width: 100%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  padding: 10px 0px 10px 20px;
  height: 50px;
  background: linear-gradient(#000000, #035838);
}
.cancel {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  text-align: center;
  font-size: 16px;
  color: #fff;
}
.box {
  -webkit-box-flex: 75%;
  -ms-flex: 75% 0 0px;
  flex: 75% 0 0;
  padding-left: 30px;
  height: 30px;
  line-height: 30px;
  border-radius: 5px;
  background: #fff;
  color: #333;
  font-size: 14px;
  caret-color: #26a2ff;
}
.icon-search {
  position: absolute;
  top: 18px;
  left: 28px;
  font-size: 14px;
  color: #777;
}
</style>
